//
//  NewsPost.swift
//  WxToutiao
//
//  Created by YQ05 on 2018/4/12.
//  Copyright © 2018年 yons. All rights reserved.
//


//import Foundation
import Moya
import ObjectMapper

//struct newsPostListResponse: Codable {
//
//    var data : [NewsData]!
//    //var thumb: [Thumb]!
//}
//
//
//struct NewsData: Codable {
//    var ID : Int?
//    var title:String?
//    //var thumb: String?
//    var img :String?
//    var modified : String?
//    //var friends: [Any]?         // Users数组
//
//}


struct newsPostListResponse2: Mappable {
    
    var data : [NewsData2]!
    
    init?(map:Map) {
        
    }
    mutating func mapping(map: Map) {
        data    <- map["data"]
    }
    
}

struct NewsData2: Mappable {
    var ID : Int?
    var title:String?
    
    var img: [Any]?
    var image :String?
    
    var modified : String?
    var content :String?
    
    
    init?(map: Map) {
        
    }
    
    mutating func mapping(map: Map) {
        ID     <- map["ID"]
        title     <- map["title"]
        
        img     <- map["thumb"]
        image = img![0] as? String
        
        modified     <- map["modified"]
        content     <- map["content"]
    }
}







extension NewsData2 {
    /// 获取分类下的文章列表
    static func request(id: Int,completion: @escaping ([NewsData2]?) -> Void ) {

        let provider = MoyaProvider<NetworkService2>()

        provider.request(.getNewsCateList(id: id)) { (result) in
            switch result {
            case let .success(moyaResponse):

                do {
//                    let decoder = JSONDecoder()
//
//                    let postIndexResponse = try decoder.decode(newsPostListResponse2.self, from: moyaResponse.data)
//                    print("news文章列表json:", postIndexResponse)
//                    completion(postIndexResponse)
                    
                    let json = try moyaResponse.mapJSON() as! [String:Any]
                    let jsonResponse = newsPostListResponse2(JSON: json)
                    print("文章标题数量NewsPost.swift \(String(describing: jsonResponse!.data.count))")
                    completion(jsonResponse?.data)
                    
                    
                } catch  {
                    print("NewsPost.swift文章列表json解析错误：",error)
                }



            case .failure:
                print("网络错误")
                completion(nil)

            }
        }
}
}

//func homeNewsrequest(id: Int,completion: @escaping ([title]?) -> Void ) {
//    
//    let provider = MoyaProvider<NetworkService2>()
//    
//    provider.request(.getNewsCateList(id: id)) { (result) in
//        switch result {
//        case let .success(moyaResponse):
//            
//            do {
//                let decoder = JSONDecoder()
//                
//                let postIndexResponse = try decoder.decode(newsPostIndexResponse.self, from: moyaResponse.data)
//                print("文章列表json:", postIndexResponse)
//                completion(postIndexResponse)
//            } catch  {
//                print("文章列表json解析错误：",error)
//            }
//            
//            
//            
//        case .failure:
//            print("网络错误")
//            completion(nil)
//            
//        }
//    }
//}


